package com.config.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.config.entity.LineConfig;
import com.config.entity.TreeModel;
import com.config.service.LineConfigService;

@Controller
@RequestMapping("/lineConfig")
@Scope("prototype")
public class LineConfigController {
	
	@Resource(name="lineConfigService")
	private LineConfigService service;
	
	/**
	 * 生产线界面
	 * @return
	 */
	@RequestMapping("/line")
	public String line(Model model){
		model.addAttribute("currentName","生产线配置");
		return "config/lineConfig";
	}
	
	/**
	 * 车间生产线treegrid分页显示
	 */
	@RequestMapping("/findByTreePage")
	public Model findAll(Model model,
			@RequestParam(value="id",required=false)Integer id,
			@RequestParam(value = "page", required = false, defaultValue="0")int start,
			@RequestParam(value = "rows", required = false, defaultValue="10")int limit){
		List<LineConfig> list=service.findByTreePage(id,start,limit);
		List<LineConfig> all = new ArrayList<LineConfig>();
		LineConfig entity=null;
		for(LineConfig e:list){
			entity=e;
			if("N".equals(e.getIsLine())){
				entity.setIsLine("否");
			}else if("Y".equals(e.getIsLine())){
				entity.setIsLine("是");
			}
			if("N".equals(e.getIsCollect())){
				entity.setIsCollect("未采集");
			}else if("Y".equals(e.getIsCollect())){
				entity.setIsCollect("已采集");
			}else{
				entity.setIsCollect(null);
			}
			List<LineConfig> t=e.getChildren();
			if(t!=null){
				for(int i=0;i<t.size();i++){
					if("N".equals(e.getChildren().get(i).getIsLine())){
						entity.getChildren().get(i).setIsLine("否");
					}else if("Y".equals(e.getChildren().get(i).getIsLine())){
						entity.getChildren().get(i).setIsLine("是");
					}
					if("N".equals(e.getChildren().get(i).getIsCollect())){
						entity.getChildren().get(i).setIsCollect("未采集");
					}else if("Y".equals(e.getChildren().get(i).getIsCollect())){
						entity.getChildren().get(i).setIsCollect("已采集");
					}else{
						entity.setIsCollect(null);
					}
				}
			}
			all.add(entity);
		}
		int total=service.countByTreePage(id);
		model.addAttribute("rows",all);
		model.addAttribute("total",total);
		return model;
	}
	
	/**
	 * 根据id加载指定对象
	 */
	@RequestMapping("/load")
	public Model load(Model model,int id){
		LineConfig line=service.getLineConfig(id);
		if(line!=null){
			model.addAttribute("success",true);
			model.addAttribute("data",line);
		}else{
			model.addAttribute("success",false);
			model.addAttribute("msg","系统故障");
		}
		return model;
	}
	
	/**
	 * 保存生产线（车间）
	 */
	@RequestMapping("/save")
	public Model save(Model model,LineConfig line){
		line.setCreateTime(new Date());
		if(line.getIsLine().equals("N")){
			List<LineConfig> shop=service.findAllWorkshop();
			if(shop.size()>4){
				model.addAttribute("success",false);
				model.addAttribute("msg","测试，只建一个车间");	
			}else{
				service.save(line);
				model.addAttribute("success",true);
			}
		}else{
			List<LineConfig> lines=service.listByWorkShop(line.getFatherId());
			if(lines.size()>0){
				model.addAttribute("success",false);
				model.addAttribute("msg","测试，只建一个生产线");	
			}else{
				LineConfig check=service.getByLineNo(line.getLinesNo());
				if(check!=null){
					model.addAttribute("success",false);
					model.addAttribute("msg","生产线标号有重复，请重新编辑");
				}
				service.save(line);
				model.addAttribute("success",true);
			}
		}
		return model;
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public Model delete(Model model,Integer id){
		List<LineConfig> childList=service.listByWorkShop(id);
		if(childList.size()>0){
			model.addAttribute("success",false);
			model.addAttribute("msg","操作失败,请先删除相关数据！");
		}else{
			service.delete(id);
			model.addAttribute("success",true);
			model.addAttribute("msg","删除成功");
		}
		return model;
	}
	
	/**
	 * 生成生产线树
	 * @param model
	 * @return
	 */
	@RequestMapping("/generateTree")
	public @ResponseBody List<TreeModel> generateTree(Model model){
		return service.generateTree();
	}
	
	/**
	 * 更新
	 * @param model
	 * @param line
	 * @return
	 */
	@RequestMapping("/update")
	public Model update(Model model,LineConfig line){
		line.setCreateTime(new Date());
		service.update(line);
		model.addAttribute("success",true);
		return model;
	}
	
	/**
	 * 获取所有生产线的下拉框数据
	 * @return
	 */
	@RequestMapping("/getComboboxValue")
	public List<Map<String,Object>> getComboboxValue(){
		List<LineConfig> list=service.findAllLine();
		List<Map<String,Object>> value=new ArrayList<Map<String,Object>>();
		if(list.size()>0){
			for(LineConfig e:list){
				Map<String,Object> map=new HashMap<String,Object>();
				map.put("id", e.getId());
				map.put("text", e.getName());
				value.add(map);
			}
		}
		return value;
	}
}
